<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { luckDrawApi, propApi } from '@/api/controller';
  import { ToastSuccess, ToastWarning, Toastloading, allNumber } from '@/utils/public';
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:'',
    pool_id:0,
    extract_pool_id:0,
    status:0,
    b_pool_name:'',
    b_status:0,
    pool_list:[] as Array<poolQuery>,
    options:[] as Array<any>,
  })

  interface poolQuery {
    id:number
    item_no:number
    gift_id:number
    gift_name:string
    gift_price:number
    less_num:boolean
    extract_num:number
  }
  interface formQuert {
    extract_pool_id:number
    extract_info:string
  }
  const handleSubmit = async()=>{
    const list = [] as Array<any>
    let num = 0
    for(let res of action.pool_list) {
      const { item_no=0, gift_id=0, extract_num=0 } = res
      if(extract_num != 0 && num ==0) num+=1
      list.push({
        item_no,
        gift_id,
        extract_num
      })
    }
    if(num == 0) return ToastWarning('请输入抽取数量')
    const loading = Toastloading()
    let form = {
      extract_pool_id:action.extract_pool_id,
      extract_info:JSON.stringify(list)
    } as formQuert
    luckDrawApi.wishWheelAddExtractPoolGift(form).then(()=>{
      loading.close()
      ToastSuccess('保存抽取礼物池配置成功')
      closeDialog()
    }).catch(()=>{
      loading.close()
    })
  }
  const getPoolList = async()=>{
    const loading = Toastloading()
    const res = await luckDrawApi.wishWheelPoolConfigNum({
      pool_id:action.pool_id
    })
    loading.close()
    const { data=[] } = res
    action.pool_list = data.map((res:any)=>{
      const { extract_num } = res
      return {
        ...res,
        extract_num:extract_num?extract_num:0
      }
    })
  }
  const openDialog = async(query:any={})=> {
    const { id, pool_id, status, options=[], pool_name='', b_pool_name='', b_status=0 } = query
    action.options = options
    action.status = status
    action.b_pool_name = b_pool_name
    action.b_status = b_status
    action.extract_pool_id = id
    action.pool_id = pool_id
    action.title = `礼物池：${pool_name}`
    await getPoolList()
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="5vh"
      class="xm-dialog"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :close-on-press-escape="action.disabled"
      width="50%"
      :title="action.title">
      <div class="xm-headTab">
        <div class="labelBox input-width">
          <div class="labelItem">
            <div class="label">抽取礼物池状态：</div>
            <div class="lable-val">
              <el-tag
                :type="['','success','warning'][action.status] || ''"
              >
                {{ action.options.find(res=>res.type == action.status)?.label || action.status }}
              </el-tag>
            </div>
          </div>
          <div class="labelItem">
            <div class="label">关联礼物池：</div>
            <div class="lable-val">{{ action.b_pool_name }}次</div>
          </div>
          <div class="labelItem">
            <div class="label">关联礼物池状态：</div>
            <div class="lable-val">
              <el-tag
                :type="['','success','warning'][action.b_status] || ''"
              >
                {{ action.options.find(res=>res.type == action.b_status)?.label || action.b_status }}
              </el-tag>
            </div>
          </div>
        </div>
      </div>
      <el-table
        class="xm-TableCenter"
        :data="action.pool_list"
        height="500"
        border
      >
        <el-table-column width="80" prop="gift_id" label="礼物ID" />
        <el-table-column min-width="120" prop="gift_name" label="礼物名称" />
        <el-table-column min-width="80" prop="gift_price" label="礼物价格" />
        <el-table-column min-width="80" prop="less_num" label="可抽取数量" />
        <el-table-column width="150" label="抽取数量">
          <template #default="scoped">
            <el-input
              @input="allNumber(scoped.row,'extract_num')"
              v-model="scoped.row.extract_num"
              placeholder="请输入抽取数量" />
          </template>
        </el-table-column>
      </el-table>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">保存礼物池配置</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
